package cn.com.wxd.amumu.scheduler.ctl;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Title:Ajax请求返回的Json包装类
 * Description:Ajax请求返回的Json包装类
 *
 * Company: Amumu管理平台
 * Copyright: Copyright (c) 2015
 * All right reserved.
 * Created by ComXu on 2017/5/19.
 *
 * @author WangXuDong
 * @version 1.0
 */
public class AjaxResult {
    private static final Logger log = LoggerFactory.getLogger(AjaxResult.class);
    public static final Integer FAILURE = -1;
    public static final Integer SUCCESS = 0;
    public static final Integer ERROR = 1;

    private int errNo;
    private Object data;
    private String message;
    private static boolean success;

    /**
     * 返回成功结果
     * @param data
     * @return
     */
    public static AjaxResult successResult(Object data) {
        AjaxResult result = new AjaxResult();
        result.setErrNo(SUCCESS);
        result.setData(data);
        success = true;
        return result;
    }

    /**
     * 返回失败的结果
     * @param data
     * @param message
     * @return
     */
    public static AjaxResult failureResult(Object data,String message) {
        AjaxResult result = new AjaxResult();
        result.setErrNo(FAILURE);
        result.setData(data);
        result.setMessage(message);
        success = false;
        return result;
    }

    /**
     * 返回异常结果
     * @param data
     * @param message
     * @param exception
     * @return
     */
    public static AjaxResult errorResult(Object data, String message, Exception exception) {
        log.error("", exception);
        AjaxResult result = new AjaxResult();
        result.setErrNo(ERROR);
        result.setMessage(message + "Error:" + exception.getMessage());
        result.setData(data);
        success = false;
        return result;
    }

    /**
     * 成功结果
     * @return
     */
    public static AjaxResult successResult() {
        AjaxResult result = new AjaxResult();
        result.setErrNo(SUCCESS);
        success = true;
        return result;
    }

    /**
     * 失败结果
     * @param message
     * @return
     */
    public static AjaxResult failureResult(String message) {
        AjaxResult result = new AjaxResult();
        result.setErrNo(FAILURE);
        result.setMessage(message);
        success = false;
        return result;
    }

    /**
     * 错误结果
     * @param message
     * @param exception
     * @return
     */
    public static AjaxResult errorResult(String message, Exception exception) {
        if(log.isErrorEnabled()){
            log.error("", exception);
        }
        AjaxResult result = new AjaxResult();
        result.setErrNo(ERROR);
        success = false;
        result.setMessage(message + "Error:" + exception.getMessage());
        return result;
    }

    /**
     * 返回给客户端String 数据
     * @param content
     * @param response
     * @throws IOException
     */
    public static void sendAjax(String content, HttpServletResponse response) throws IOException {
        response.setContentType("application/json; charset=UTF-8");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.getWriter().write(content);
        response.getWriter().flush();
    }

    public int getErrNo() {
        return errNo;
    }

    public void setErrNo(int errNo) {
        this.errNo = errNo;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isSuccess(){
        return success;
    }
}
